package com.mdc.mansion.common;

import com.mdc.mansion.common.util.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class Md5RsaUtils {
	
	private static final String SIGN_ALGORITHMS = "MD5WithRSA";
	
	/**
	 * RSA验签名检查
	 *
	 * @param content       待签名数据
	 * @param sign          签名值
	 * @param input_charset 编码格式
	 * @return 布尔值
	 */
	public static boolean verify(String content, String sign, String iapp_pub_key, String input_charset) {
		try {
			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
			byte[] encodedKey = Base64.decode(iapp_pub_key);
			PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);
			
			signature.initVerify(pubKey);
			signature.update(content.getBytes(input_charset));
			
			return signature.verify(Base64.decode(sign));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}
	
	public static String sign(String content, String priKey, String input_charset) {
		try {
			PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(priKey));
			KeyFactory keyf = KeyFactory.getInstance("RSA");
			PrivateKey priKey1 = keyf.generatePrivate(priPKCS8);
			
			java.security.Signature signature = java.security.Signature
					.getInstance(SIGN_ALGORITHMS);
			
			signature.initSign(priKey1);
			signature.update(content.getBytes(input_charset));
			
			byte[] signed = signature.sign();
			
			return Base64.encode(signed);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	//用md5生成内容摘要，再用RSA的私钥加密，进而生成数字签名
	static String getMd5Sign(String content , PrivateKey privateKey,String input_charset) throws Exception {
		
		byte[] contentBytes = content.getBytes(input_charset);
		Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
		signature.initSign(privateKey);
		signature.update(contentBytes);
		byte[] signs = signature.sign();
		return Base64.encode(signs);
	}
	
	//对用md5和RSA私钥生成的数字签名进行验证
	static boolean verifyWhenMd5Sign(String content, String sign, PublicKey publicKey,String input_charset) throws Exception {
		byte[] contentBytes = content.getBytes(input_charset);
		Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
		signature.initVerify(publicKey);
		signature.update(contentBytes);
		return signature.verify(Base64.decode(sign));
	}
	
	//用sha1生成内容摘要，再用RSA的私钥加密，进而生成数字签名
	static String getSha1Sign(String content , PrivateKey privateKey,String input_charset) throws Exception {
		byte[] contentBytes = content.getBytes(input_charset);
		Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
		signature.initSign(privateKey);
		signature.update(contentBytes);
		byte[] signs = signature.sign();
		return Base64.encode(signs);
	}
	
	//对用md5和RSA私钥生成的数字签名进行验证
	static boolean verifyWhenSha1Sign(String content, String sign, PublicKey publicKey,String input_charset) throws Exception {
		byte[] contentBytes = content.getBytes(input_charset);
		Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
		signature.initVerify(publicKey);
		signature.update(contentBytes);
		return signature.verify(Base64.decode(sign));
	}
	

}
